package com.sunchangpeng.wechat.mp.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.sunchangpeng.wechat.core.WechatClient;
import com.sunchangpeng.wechat.mp.EndpointProvider;
import com.sunchangpeng.wechat.mp.MpAppSetting;
import com.sunchangpeng.wechat.mp.MpWechatClientFactory;
import com.sunchangpeng.wechat.mp.user.bean.Tag;
import com.sunchangpeng.wechat.mp.user.bean.UserPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

@Slf4j
public class Tags {
    private WechatClient wechatClient;

    public void setWechatClient(WechatClient wechatClient) {
        this.wechatClient = wechatClient;
    }

    public static Tags with(MpAppSetting appSetting) {
        Tags tags = new Tags();
        tags.setWechatClient(MpWechatClientFactory.getInstance().with(appSetting));
        return tags;
    }

    /**
     * 创建标签
     */
    public Tag create(String name) {
        String url = EndpointProvider.get("url.tag.create");
        TagWrapper tag = new TagWrapper(name);
        String response = wechatClient.getTextWithToken(url, JSON.toJSONString(tag));
        return JSON.parseObject(response, TagWrapper.class).getTag();
    }


    /**
     * 获取公众号已创建的标签
     */
    public List<Tag> get() {
        String url = EndpointProvider.get("url.tag.list");
        String response = wechatClient.getTextWithToken(url);
        return JSON.parseObject(response, TagList.class).getTags();
    }

    /**
     * 编辑标签
     */
    public void update(Tag tag) {
        String url = EndpointProvider.get("url.tag.update");
        wechatClient.getTextWithToken(url, JSON.toJSONString(new TagWrapper(tag)));
    }

    /**
     * 删除标签
     */
    public void delete(int id) {
        String url = EndpointProvider.get("url.tag.delete");
        wechatClient.getTextWithToken(url, JSON.toJSONString(new TagWrapper(id)));
    }

    /**
     * 获取标签下粉丝列表
     */
    public UserPage getUsers(int tagId) {
        return getUsers(tagId, null);
    }

    /**
     * 获取标签下粉丝列表
     */
    public UserPage getUsers(int tagId, String nextOpenId) {
        String url = EndpointProvider.get("url.tag.user.list");
        JSONObject json = new JSONObject().fluentPut("tagid", tagId);
        if (StringUtils.isNotBlank(nextOpenId)) {
            json.put("next_openid", nextOpenId);
        }
        String response = wechatClient.getTextWithToken(url, JSON.toJSONString(json));
        return JSON.parseObject(response, UserPage.class);
    }

    /**
     * 批量为用户打标签
     */
    public void batchtagging(int tagId, List<String> openIds) {
        String url = EndpointProvider.get("url.tag.user.tag");
        JSONObject data = new JSONObject()
                .fluentPut("tagid", tagId)
                .fluentPut("openid_list", openIds);
        wechatClient.getTextWithToken(url, JSONObject.toJSONString(data));
    }

    /**
     * 批量为用户取消标签
     */
    public void batchuntagging(int tagId, List<String> openIds) {
        String url = EndpointProvider.get("url.tag.user.untag");
        JSONObject data = new JSONObject()
                .fluentPut("tagid", tagId)
                .fluentPut("openid_list", openIds);
        wechatClient.getTextWithToken(url, JSON.toJSONString(data));
    }

    /**
     * 获取用户身上的标签列表
     *
     * @param openId openId
     * @return
     */
    public List<Integer> getUserTags(String openId) {
        String url = EndpointProvider.get("url.tag.user.gettag");
        String json = JSON.toJSONString(new JSONObject().fluentPut("openid", openId));
        String response = wechatClient.getTextWithToken(url, json);
        return JSON.parseObject(response, TagIdsWrapper.class).getList();
    }

    static class TagList {
        @JSONField(name = "tags")
        private List<Tag> tags;

        public List<Tag> getTags() {
            return tags;
        }

        public void setTags(List<Tag> tags) {
            this.tags = tags;
        }
    }

    static class TagWrapper {
        private Tag tag;

        public TagWrapper() {
        }

        public TagWrapper(long id) {
            Tag tag = new Tag();
            tag.setId(id);
            this.tag = tag;
        }

        public TagWrapper(String name) {
            Tag tag = new Tag();
            tag.setName(name);
            this.tag = tag;
        }

        public TagWrapper(Tag tag) {
            this.tag = tag;
        }

        public Tag getTag() {
            return tag;
        }

        public void setTag(Tag tag) {
            this.tag = tag;
        }
    }

    static class TagIdsWrapper {
        @JSONField(name = "tagid_list")
        private List<Integer> list;

        public List<Integer> getList() {
            return list;
        }

        public void setList(List<Integer> list) {
            this.list = list;
        }
    }
}
